<template>
  <div>
    <el-dialog :title="form.id?'编辑':'添加'" :visible.sync="buffer.Bufferfrom">
      <el-form :model="form">
        <el-form-item label="属性名" :label-width="formLabelWidth">
          <el-input v-model="form.specsname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          v-for="(item, index) in attrs"
          :key="index"
          label="规格值"
          :label-width="formLabelWidth"
          
        >
          <el-input v-model="item.value" style="width:70%"></el-input>
          <el-button @click="addDomain" v-if="index == 0" style="width:30%">新增域名</el-button>
          <el-button @click="addDel(index)" v-else style="width:30%">删除</el-button>
        </el-form-item>
        <el-form-item label="属性状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="buffer.Bufferfrom = false">取 消</el-button>
        <el-button type="primary" @click="sumit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["buffer", "roleList"],
  name: "",
  components: {},
  data() {
    return {
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        specsname: "",
        attrs: "",
        status: "",
      },
      formLabelWidth: "120px",
      attrs: [{ value: "" }],
    };
  },
  created() {},
  watch: {
    buffer: {
      handler(newV, oldV) {
        if (newV.Bufferfrom == false) {
          this.form = {
            specsname: "",
            attrs: "",
            status: "",
          };
          this.attrs[{ value: "" }];
        }
      },
    },
  },
  mounted() {},
  methods: {
    addDomain() {
      this.attrs.push({ value: "" });
    },
    addDel(index) {
      this.attrs.splice(index, 1);
    },
    sumit() {
      this.form.attrs = this.attrs.map((item) => item.value).join(",");
      if (this.form.id) {
         this.$api.specsEdit(this.form).then((res) => {
          console.log(res);
          this.buffer.Bufferfrom = false;
          this.$emit("change");
        });
      } else {
        this.$api.specsAdd(this.form).then((res) => {
          console.log(res);
          this.buffer.Bufferfrom = false;
          this.$emit("change");
        });
      }
    },
  },
};
</script>
<style scoped lang="scss"></style>
